constructive algorithms math number theory

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

#define ll long long int
#define f(i,x,n) for(ll i = x; i < n; i++)
#define fn for(ll i = 0; i < n; i++)
#define rf(i,x,n) for(ll i = n; i >= x; i--)
#define fr(x,m) for(auto &x:m)
#define Kuch_toh_gadbad_hai_re_baba ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define vll vector<ll>
#define p(n) cout<<n<<"\n"
#define pb push_back

using namespace std;

const ll N = 1e5+10;

vector<ll> adj[N];
vector<ll> vis(N);

void dfs(ll i,vll &ans){
    vis[i]=1;
    ans.pb(i);

    for(auto it:adj[i]){
        if(!vis[it]) dfs(it,ans);
    }
}


void solve(){
    ll n;cin>>n;

    f(i,0,n+10){
        adj[i].clear();
        vis[i]=0;
    }

    rf(i,1,n){
        if(i%2==0){
        adj[i].pb(i/2);
        adj[i/2].pb(i);
        }
        
    }

    vll ans;
    rf(i,1,n){
        if(!vis[i]) dfs(i,ans);
    }

    for(auto it:ans) cout<<it<<" ";
    cout<<endl;

}

int main(){    
    Kuch_toh_gadbad_hai_re_baba;

   ll t;cin>>t; while(t--) 
   solve();
} 


Comments

Submit
0 Comments
More Questions

405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons